// pages/index/acrossBrand/acrossBrand.js
import {
        list
} from '../../../../api/brand'
import {
        list as productList
} from '../../../../api/product'
const util = require('../../../../utils/util.js')
let app = getApp();
Component({
        /**
   * 组件的属性列表
   */
        properties: {
                info: {
                        type: Object,
                }
        },


        /**
         * 组件的初始数据
         */
        data: {
                brandId: '',
                winWidth: 0,
                winHeight: 0,
                currentTab: 0,
                toView: '',
                brandList: [],
                brandProducts: [],
                products: [],
                tagIds: [],
        },
        lifetimes: {
                attached() {
                        if (this.data.info.tagids != null) {
                                this.data.tagIds = this.data.info.tagids.split(',')
                                for (var i = 0; i < this.data.tagIds.length; i++) {
                                  this.data.tagIds[i] = parseInt(this.data.tagIds[i])
                                }
                              }
                                this.setData({
                                        tagIds: this.data.tagIds
                                })
                        
                        this.loadBrand()
                        this.loadProduct()
                }
        },
        /**
         * 组件的方法列表
         */
        created() {
                this.loadBrand()



        },
        attached() {

                var that = this;
                /**
                 * 获取当前设备的宽高
                 */
                wx.getSystemInfo({
                        success: function (res) {
                                that.setData({
                                        winWidth: res.windowWidth,
                                        winHeight: res.windowHeight
                                });
                        }

                });
        },
        methods: {
                toGoods(e) {
                        wx.navigateTo({
                                url: '/pages/goods/product/details?id=' + e.currentTarget.dataset.id
                        })
                },
                loadProduct() {
                        productList({
                                brandId: this.data.brandId,
                                mchId: app.globalData.mchId
                        }).then(res => {
                                this.data.products = res.data.data
                                this.data.products.forEach(item => {

                                        item.thumbnail = util.thumbnail(item.thumbnail, 206, 206)
                                })
                                this.setData({
                                        products: this.data.products
                                })
                        })
                },
                loadBrand() {
                        list({
                                mchId: app.globalData.mchId,
                                tagIds: this.data.tagIds,
                        }).then(res => {
                                this.data.brandList = res.data.data
                                this.data.brandId = this.data.brandList[0].id

                                this.setData({
                                        brandList: this.data.brandList
                                })
                                this.loadProduct()
                        })
                },
                //  tab切换逻辑
                swichNav: function (e) {
                        this.data.brandId = e.currentTarget.dataset.name
                        var that = this;
                        this.data.brandProducts = []
                        this.loadProduct()


                        if (that.data.currentTab === e.currentTarget.dataset.current) {
                                return false;
                        } else {
                                that.setData({
                                        currentTab: e.currentTarget.dataset.current
                                })
                        }
                },
                bindChange: function (e) {
                        var that = this;
                        that.setData({
                                currentTab: e.detail.current
                        });
                },
                changeTab(e) {
                        console.log(e)
                        if (this.tabIndex === e.currentTarget.dataset.index) {
                                return;
                        }
                        this.tabIndex = e.currentTarget.dataset.index
                        this.scrollinto = 'tab' + e.currentTarget.dataset.index
                },

        }
})